package clothes.tools.web;

import clothes.tools.pojo.Sell;
import clothes.tools.service.SellService;
import clothes.tools.service.impl.SellServiceImpl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.List;

public class SellServlet extends BaseServlet{
    private SellService sellService=new SellServiceImpl();
    /**
     * 添加订单信息
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }

    /**
     * 删除订单信息
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String sid = req.getParameter("sid");
        int sid1 = Integer.parseInt(sid);
        sellService.deleteSellById(sid1);
        resp.getWriter().write("success");
    }

    /**
     * 更新订单信息
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType ("text/html;charset=UTF-8");
        String sid = req.getParameter("sid");
        String pid=req.getParameter("pid");
        String pname=req.getParameter("pname");
        String pcolor=req.getParameter("pcolor");
        String psize=req.getParameter("psize");
        String price=req.getParameter("price");
        String onum=req.getParameter("onum");
        String cid = req.getParameter("cid");
        String adress = req.getParameter("adress");
        String phone = req.getParameter("phone");
        String otime = req.getParameter("otime");
        String name = req.getParameter("name");
        String totalprice = req.getParameter("totalprice");
        int sid1 = Integer.parseInt(sid);
        int onum1=Integer.parseInt(onum);
        sellService.updateSell(new Sell(sid1,pid,otime,onum1,pcolor,psize,pname,cid,new BigDecimal(price),new BigDecimal(totalprice),adress,phone,name));
        resp.getWriter().write("success");
    }

    /**
     * 展示所有订单信息
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pageStr=req.getParameter("page");
        String limitStr=req.getParameter("limit");
//        1、通过CommodityServlet查询全部商品
        List<Sell> sell = sellService.querySell();
        List<Sell> sells=sellService.querySell(pageStr,limitStr);
        resp.setContentType ("text/html;charset=UTF-8");
        JSONObject jsonObject = new JSONObject ();
        //接收的数据
        JSONArray data = JSONArray.parseArray (JSON.toJSONString (sells));
        jsonObject.put ("data", data);
        jsonObject.put ("code", 0);
        jsonObject.put ("msg", "");
        //获得数据总数
        jsonObject.put ("count", sell.size ());
        PrintWriter writer = resp.getWriter ();
        writer.print (jsonObject.toJSONString ());
    }

    /**
     * 模糊查询订单信息
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void select(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pageStr=req.getParameter("page");
        String limitStr=req.getParameter("limit");
        String select = req.getParameter("select");
        List<Sell> sell = sellService.querySell();
        List<Sell> sells=sellService.querySelectSell(pageStr,limitStr,select);
        resp.setContentType ("text/html;charset=UTF-8");
        JSONObject jsonObject = new JSONObject ();
        JSONArray data = JSONArray.parseArray (JSON.toJSONString (sells));
        jsonObject.put ("data", data);
        jsonObject.put ("code", 0);
        jsonObject.put ("msg", "");
        jsonObject.put ("count", sell.size ());
        PrintWriter writer = resp.getWriter ();
        writer.print (jsonObject.toJSONString ());
    }
}
